package com.CST.config;

import com.CST.interceptor.*;
import org.springframework.beans.factory.annotation.*;
import org.springframework.context.annotation.*;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @Author: wzw
 * @Date: 2020/12/15 9:18
 * @return
 */
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private TokenInterceptor tokenInterceptor;

    /**
     * 跨域
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
	  registry.addMapping("/**").allowedOrigins("*").allowedMethods("*").allowedHeaders("*")
		    .maxAge(3600L);
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(tokenInterceptor).addPathPatterns("/**").excludePathPatterns("/swagger-resources/**", "/webjars/**",
                "/v2/**", "/image/**","/swagger-ui.html");
    }

    @Value("${upload}")
    private String uploadPath;

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
	  registry.addResourceHandler("/upload/**").addResourceLocations("file:" + uploadPath);
    }

    @Bean
    public TokenInterceptor tokenInterceptor() {
	  return new TokenInterceptor();
    }
}
